/**
 * 
 */
package liwei;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
import java.util.logging.Level;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import java.awt.Toolkit;


import weibo4j.Timeline;
//import weibo4j.examples.oauth2.Log;
import weibo4j.model.Status;
import weibo4j.model.WeiboException;
import zhangzhifan.WordCounter;

/**
 * @author 李炜
 *
 */
public class MyPanel extends JPanel
{

	/**
	 * 主面板的构造函数，包括输入区、统计单词标签和发微博按钮
	 */
	public MyPanel()
	{		
		//this class is written by ZhangZhifan,which is used to count some specific words
		wordCounter = new WordCounter();
		
		mainTextArea = new MainTextArea(this,15,60);
		setLayout(new BorderLayout(0,0));
		wordNumLabel = new JLabel("单词数");
		add(wordNumLabel,BorderLayout.NORTH);
		add( new JScrollPane(mainTextArea),BorderLayout.CENTER);
		mainTextArea.setMyPanel(this);
		//设置发微博按钮的图标
		//to set the icon of publishing a weibo
		ImageIcon weiboIcon = null;
		weiboIcon = new ImageIcon("C:\\Users\\LiWei\\git\\text-editor-liwei\\weibo4j-oauth2\\src\\weibo_icon24x24.png");
		final JButton numButton = new JButton("发微博",weiboIcon);
		numButton.addMouseListener(new MouseAdapter()
		{	
			//对发微博按钮的按下做出响应
			//to publish a weibo when pressed
			public void mousePressed(MouseEvent event)
			{
				textString = mainTextArea.getText();
				//first we should check whether the lenght of the text is within 140,otherwise an exception 
				//can be touched
				numButton.setText("剩余字符数"+Integer.toString(140-textString.length()));
				if(textString.length() < 140)
				{
					weibo();
				}
				else 
				{
					JOptionPane.showMessageDialog(null, "字数超过了140！");
				}
			}
			//使得按钮标签变回原样
			//set the text of the button to its origin
			public void mouseReleased(MouseEvent event)
			{
				numButton.setText("发微博");
			}
		});
		
		//using the method written by ZhangZhifan count the number of some specific words
		final JTextField searchField = new JTextField("统计词频",10);
		searchField.addActionListener(new ActionListener()
		{
			
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				String stringToFind = searchField.getText();
				
				int wordNum = wordCounter.getFreq(stringToFind);
				searchField.setText(Integer.toString(wordNum));
			}
		});
		searchField.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e)
			{
				searchField.setText("");
			}
		});
		JPanel southPanel = new JPanel();
		add(southPanel,BorderLayout.SOUTH);
		southPanel.setLayout(new FlowLayout());
		
		JLabel label = new JLabel("统计词频");
		southPanel.add(label);
		southPanel.add(searchField);
		southPanel.add(numButton);
	}
	public MainTextArea getMainTextArea()
	{
		return mainTextArea;
	}
	public String getText()
	{
		return mainTextArea.getText();
	}
	public void setText(String str)
	{
		mainTextArea.setText(str);
	}
	public void setWordNum(int num)
	{
		wordNumLabel.setText(Integer.toString(num));
	}
	public void showWordNumLabel()
	{
		wordNumLabel.setText("单词数"+wordNum);
	}
	/**
	 * to read the  file selected and present the text to our editor
	 * @param fileToRead
	 * @throws FileNotFoundException
	 */
	public void readFile(File fileToRead) throws FileNotFoundException
	{
		//File fileToRead = new File(url);
		Scanner in = new Scanner(fileToRead);
		StringBuilder textStringBuilder = new StringBuilder();
		while(in.hasNextLine())
		{
			textStringBuilder.append(in.nextLine()+"\n");
		}
		textString = textStringBuilder.toString();
		mainTextArea.setText(textString);
		in.close();
	}
	/**
	 * 向某个输入的url地址写入文件
	 * write the text to the file selected
	 * @param url写入目标地址
	 */
	public void writeFile(File fileToWrite)
	{
		
		try
		{
			out = new PrintWriter(fileToWrite);
			String output =  mainTextArea.getText();
			out.println(output);
			
		} catch (FileNotFoundException e)
		{
			Main.logger.log(Level.ALL,"Can't save to the selected file");
			//e.printStackTrace();
		}
		finally
		{
			out.close();
		}
	}
	/**
	 * 将面板中的内容发送微博
	 * to publish a weibo with  the text of our text editor
	 */
	public void weibo()
	{
		textString = mainTextArea.getText();
		String statuses = textString;
		Timeline tm = new Timeline();
		tm.client.setToken(access_token);
		try {
			Status status = tm.UpdateStatus(statuses);
			//Log.logInfo(status.toString());
		} catch (WeiboException e) {
			Main.logger.log(Level.ALL,"Something wrong with weibo");
			//e.printStackTrace();
		}	
	}
	private MainTextArea mainTextArea;
	private String textString;
	final private JLabel wordNumLabel;
	final private String access_token = "2.004WaEGC05DXrG891bc1a7fa0j3JDf";
	private int wordNum;
	public static PrintWriter out;
	public WordCounter wordCounter;
}
